﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Windows.Resources;
using Microsoft.Xna.Framework.Audio;


namespace Sanctuary.Core.Audio
{
    public class Sounds
    {
        public const string FILE_NAME = "SoundDefinition.xml";
        private static StreamResourceInfo zipInfo;

        public Sounds(XDocument document, StreamResourceInfo zip)
        {
            zipInfo = zip;

            foreach (XElement item in document.Descendants("Sound"))
            {
                Sound s = new Sound() { 
                    Name = item.Attribute("Name").Value, 
                    IsLooped = bool.Parse(item.Attribute("IsLooped").Value), 
                    Path = item.Value };

                soundItems.Add(s.Name, s);
            }
        }

        private Dictionary<string, Sound> soundItems = new Dictionary<string, Sound>();

        public Sound this[string soundName]
        {
            get
            {
                if (!soundItems.ContainsKey(soundName))
                {
                    throw new GameContentException("Sound: " + soundName + " not conatined in " + FILE_NAME);
                }
                return soundItems[soundName];
            }
        }

        private SoundEffect effect;
        private SoundEffectInstance effectInstance;
        private MediaElement mp3Media; 

        public void PlaySound(string name)
        {
            if (effectInstance != null && effectInstance.State == SoundState.Playing)
            {
                effectInstance.Stop();
            }
            if (mp3Media != null)
            {
                mp3Media.MediaEnded -= mp3Media_MediaEnded;
                mp3Media.Stop();
                mp3Media = null;
            }

            Sound sound = this[name];

            StreamResourceInfo sr = Application.GetResourceStream(zipInfo, new Uri(sound.Path, UriKind.Relative));
            if (sound.IsWav)
            {
                effect = SoundEffect.FromStream(sr.Stream);

                effectInstance = effect.CreateInstance();
                effectInstance.IsLooped = sound.IsLooped;
                effectInstance.Play();
            }
            else
            {
                mp3Media = new MediaElement();
                mp3Media.SetSource(sr.Stream);
                mp3Media.AutoPlay = false;
                if (sound.IsLooped)
                {
                    mp3Media.MediaEnded += new RoutedEventHandler(mp3Media_MediaEnded);
                }
                mp3Media.Play();
            }
        }

        void mp3Media_MediaEnded(object sender, RoutedEventArgs e)
        {
            var mp3Media = sender as MediaElement;
            mp3Media.Position = TimeSpan.Zero;
            mp3Media.Play(); 
        }
    }
}
